<!doctype html>
<html class="no-js">

<head>
    <title>幼儿园</title>
    <#include "/boya/template/meta.html" />
</head>

<body>
    <#import "/boya/template/marc.html" as mc />
    <@mc.content>
        <div id="app" class="page">
            <div class="title">物品申领及库存(校服被子等) /</div>
            <div style="margin-bottom: 10px; display: flex; align-items: center;">
                <el-date-picker v-model="daterange" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd" unlink-panels="true" :default-value="new Date">
                </el-date-picker>
                <el-select v-model="schoolId" clearable filterable placeholder="配送点" v-if="${school.role!0} == 1">
                    <el-option v-for="item in schools" :key="item.attrs.id" :label="item.attrs.name" :value="item.attrs.id">
                    </el-option>
                </el-select>
                <!-- <el-select v-model="storageId" clearable filterable placeholder="配送产品" @change="onStorageChange" v-if="${school.role!0} == 1">
                    <el-option v-for="item in storages" :key="item.attrs.id" :label="item.attrs.name" :value="item.attrs.id">
                    </el-option>
                </el-select> -->
                <el-input v-model="name" placeholder="物品名称" style="width: 210px;" clearable></el-input>
                <el-input v-model="type" placeholder="分类" style="width: 210px;" clearable></el-input>
                <el-input v-model="size" placeholder="型号" style="width: 210px;" clearable></el-input>
                <el-button @click="onSearch"><i class="el-icon-search"></i>搜索</el-button>
            </div>
            <div class="am-btn-group am-btn-group-sm btn-group">
                <button type="button" class="am-btn am-btn-default" @click="add" v-if="${school.role!0} == 1"><i class="am-icon-plus"></i>&nbsp;新增</button>
                <button type="button" class="am-btn am-btn-default" @click="logs">使用日志</button>
            </div>
            <table class="am-table table">
                <thead>
                    <tr>
                        <th>物品类别</th>
                        <th>物品名称</th>
                        <th>分类</th>
                        <th>型号</th>
                        <th>数量</th>
                        <th>备注</th>
                        <th>单位</th>
                        <th>录入时间</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="(item, index) in list">
                        <td class="am-text-middle">{{ item.category }}</td>
                        <td class="am-text-middle">{{ item.name }}</td>
                        <td class="am-text-middle">{{ item.type }}</td>
                        <td class="am-text-middle">{{ item.size }}</td>
                        <td class="am-text-middle">{{ item.count }}</td>
                        <td class="am-text-middle">{{ item.remark }}</td>
                        <td class="am-text-middle">{{ item.schoolName }}</td>
                        <td class="am-text-middle">{{ item.addTime }}</td>
                        <td class="am-text-middle">
                            <button class="am-btn am-btn-default am-btn-xs" @click="update(item.id, 1)" v-if="${school.id!} == item.schoolId">使用</button>
                            <button class="am-btn am-btn-default am-btn-xs" @click="update(item.id, 0)">入库</button>
                            <button class="am-btn am-btn-default am-btn-xs" @click="edit(item.id)" v-if="${school.role!0} == 1">编辑</button>
                            <button class="am-btn am-btn-danger am-btn-xs" @click="del(item.id)"><i class="am-icon-trash"></i>&nbsp;删除</button>
                        </td>
                    </tr>
                </tbody>
            </table>
            <#include "/boya/template/page.html" />
        </div>
    </@mc.content>
    <script type="text/javascript">
    var app = new Vue({
        el: '#app',
        data() {
            return {
                list: [],
                daterange: '',
                schools: [],
                storages: [],
                schoolId: '',
                storageId: null,
                name: '',
                type: '',
                size: ''
            }
        },
        mounted: function() {
            var jsonStr = '${list!}';
            if (jsonStr != '') {
                this.list = ${list!}
            }
            var schoolStr = '${schools!}';
            if (schoolStr != '') {
                this.schools = ${schools!'[]'}
            }
            var storageStr = '${storages!}';
            if (storageStr != '') {
                this.storages = ${storages!'[]'}
            }
            var schoolId = '${schoolId!""}'
            if (schoolId != '') {
                this.schoolId = Number(schoolId);
            }
            var storageId = '${storageId!""}'
            if (storageId != '') {
                this.storageId = Number(storageId);
            }
            var name = '${name!""}'
            if (name != '') {
                this.name = name;
            }
            var type = '${type!""}'
            if (type != '') {
                this.type = type;
            }
            var size = '${searchSize!}'
            if (size != '') {
                this.size = size;
            }
            var startTime = '${startTime!}'
            if (startTime != '') {
                this.daterange = new Array;
                this.daterange.push('${startTime!}');
                this.daterange.push('${endTime!}');
            }
        },
        methods: {
            add() {
                layer.open({
                    type: 2,
                    area: ['700px', '550px'],
                    fixed: false, //不固定
                    maxmin: true,
                    content: '/boya/stockInfo'
                });
            },
            logs() {
                window.location.href = "/boya/stockLog";
            },
            edit(id) {
                layer.open({
                    type: 2,
                    area: ['700px', '550px'],
                    fixed: false, //不固定
                    maxmin: true,
                    content: '/boya/stockInfo?id=' + id
                });
            },
            update(id, type) {
                layer.open({
                    type: 2,
                    area: ['700px', '400px'],
                    fixed: false, //不固定
                    maxmin: true,
                    content: '/boya/useStockForm?id=' + id + '&type=' + type
                })
                // layer.prompt({ title: type == 0 ? '请输入入库数量' : '请输入使用数量', formType: 0 }, function(count, index) {
                //     if (!Number(count)) {
                //         msg("请输入数字");
                //         return;
                //     }
                //     var param = Qs.stringify({
                //         id: id,
                //         count: count,
                //         type: type
                //     });
                //     axios({
                //             url: '/boya/updateStock',
                //             method: 'post',
                //             data: param,
                //             headers: {
                //                 'Content-Type': 'application/x-www-form-urlencoded'
                //             }
                //         })
                //         .then(function(response) {
                //             if (response.data.status == 200) {
                //                 // msg("删除成功");
                //                 window.location.reload();
                //             } else {
                //                 msg("修改失败：" + response.data.msg);
                //                 console.log(response.data);
                //             }
                //         })
                //         .catch(function(error) {
                //             msg("修改失败");
                //             console.log(error);
                //         });
                //     layer.close(index);
                // });
            },
            del(id) {
                confirm("确认删除？", function() {
                    var param = Qs.stringify({
                        id: id
                    });
                    axios({
                            url: '/boya/delStock',
                            method: 'post',
                            data: param,
                            headers: {
                                'Content-Type': 'application/x-www-form-urlencoded'
                            }
                        })
                        .then(function(response) {
                            if (response.data.status == 200) {
                                // msg("删除成功");
                                window.location.reload();
                            } else {
                                msg("删除失败");
                                console.log(response.data);
                            }
                        })
                        .catch(function(error) {
                            msg("删除失败");
                            console.log(error);
                        });
                })
            },
            onDateChange(val) {
                var keys = [
                    { key: 'startDate', value: val[0] },
                    { key: 'endDate', value: val[1] }
                ]
                goPage(1, keys)

                // window.location.href = "/boya/exportList?startDate=" + val[0] + "&endDate=" + val[1];
            },
            onSchoolChange(val) {
                var keys = [
                    { key: 'schoolId', value: val }
                ]
                goPage(1, keys)
            },
            onStorageChange(val) {
                var keys = [
                    { key: 'storageId', value: val }
                ]
                goPage(1, keys)
            },
            onNameChange(val) {
                var keys = [
                    { key: 'name', value: val }
                ]
                goPage(1, keys)
            },
            onSearch() {
                var self = this;
                var keys = [
                    { key: 'schoolId', value: self.schoolId },
                    { key: 'name', value: self.name },
                    { key: 'type', value: self.type },
                    { key: 'size', value: self.size }
                ]
                if (self.daterange != null && self.daterange.length > 1) {
                    keys.push({ key: 'startDate', value: self.daterange[0] });
                    keys.push({ key: 'endDate', value: self.daterange[1] });
                } else {
                    keys.push({ key: 'startDate', value: '' });
                    keys.push({ key: 'endDate', value: '' });
                }
                goPage(1, keys);
            },
            onPageChange(val) {
                goPage(val);
            }
        }
    })
    </script>
    <style type="text/css">
    .page {
        padding: 15px;
    }

    .title {
        margin-bottom: 10px;
        font-size: 1.2em;
    }

    .btn-group {
        margin-bottom: 10px;
    }

    .table {
        /*width: auto;*/
    }

    .no-border {
        border: none;
    }
    </style>
</body>

</html>